package cn.jsj.dao;

import java.util.List;
import org.hibernate.criterion.Restrictions;
import cn.jsj.pojo.TblLawyer;

/**
 * 律师信息处理类
 * @author LI
 *
 */

public class TblLawyerDAO {
	/**
	 * 验证登陆用户是否存在
	 * @param ui：登陆用户对象
	 * @return：用户对象：null：不存在
	 */
	public TblLawyer userValidate(TblLawyer ui){
		// 取得session对象
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		List userList = session.createCriteria(TblLawyer.class).add(
				Restrictions.eq("lawyerName", ui.getLawyerName()))
				.add(Restrictions.eq("lawyerPwd", ui.getLawyerPwd()))
				.add(Restrictions.eq("deleteLabel", 0))
				.list();
		// 提交事务
		tx.commit();
		session.close();
		System.out.println(userList.size());
		//如果用户存在
		if(userList.size()!=0)
		{
			TblLawyer user=(TblLawyer)userList.get(0);
		return user;
		}
		else{
			return null;
		}
		}
	
	/**
	 * 获取律师信息条数
	 * @return:律师信息条数
	 */
	public int getLawyerInfoNumb(){
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		List result=session.createCriteria(TblLawyer.class)
		.add(Restrictions.eq("deleteLabel", 0))
		.list();
		// 提交事务
		tx.commit();
		session.close();
		return result.size();
	}
	
	/**
	 * 获取指定页面要显示的律师信息
	 * @param startNumb：从第几条开始
	 * @param totalNumb：这一页显示几条
	 * @return：这一页要显示的律师信息队列
	 */
	public java.util.List<TblLawyer> getTheLawyerInfo(int startNumb,int totalNumb){
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		List result=session.createCriteria(TblLawyer.class)
		.add(Restrictions.eq("deleteLabel", 0))
		.setFirstResult(startNumb)
		.setMaxResults(totalNumb)
		.list();
		// 提交事务
		tx.commit();
		session.close();
		return result;
	}
	
	
	/**
	 * 获得要查询的律师信息
	 * @param lawyerName：律师的名字
	 * @param positionNumber：律师的职位
	 * @param positionNumber：从第几条开始
	 * @param positionNumber：这一页显示几条
	 * @return：符合条件的律师信息队列
	 */
	public java.util.List<TblLawyer> getSearchLawyerInfo(String lawyerName,int positionNumber,int startNumb,int totalNumb){
		/*
		 * 有三种情况
		 * 1.lawyerName为NULL positionNumber不为0
		 * 2.lawyerName不为NULL positionNumber为0
		 * 3.lawyerName不为NULL positionNumber不为0
		 */
		
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		//查询结果队列
		List result=new java.util.LinkedList();
		
		//第一种情况
		if(lawyerName.length()==0&&positionNumber!=0){
			result=session.createCriteria(TblLawyer.class)
			.add(Restrictions.eq("positionName", positionNumber))
			.add(Restrictions.eq("deleteLabel", 0))
			.setFirstResult(startNumb)
		    .setMaxResults(totalNumb)
			.list();
		}
		
		//第二种情况
		if(lawyerName.length()!=0&&positionNumber==0){
			result=session.createCriteria(TblLawyer.class)
			.add(Restrictions.eq("lawyerName", lawyerName))
			.add(Restrictions.eq("deleteLabel", 0))
			.setFirstResult(startNumb)
		    .setMaxResults(totalNumb)
			.list();
		}
		
		//第三种情况
		if(lawyerName.length()!=0&&positionNumber!=0){
			result=session.createCriteria(TblLawyer.class)
			.add(Restrictions.eq("lawyerName", lawyerName))
			.add(Restrictions.eq("positionName", positionNumber))
			.add(Restrictions.eq("deleteLabel", 0))
			.setFirstResult(startNumb)
		    .setMaxResults(totalNumb)
			.list();
		}
		
		// 提交事务
		tx.commit();
		session.close();
		return result;
	}
	
	
	/**
	 * 获得要查询的律师信息条数
	 * @param lawyerName：律师的名字
	 * @param positionNumber：律师的职位
	 * @return：符合条件的律师信息条数
	 */
	public int getSearchLawyerInfoNumb(String lawyerName,int positionNumber){
		/*
		 * 有三种情况
		 * 1.lawyerName为NULL positionNumber不为0
		 * 2.lawyerName不为NULL positionNumber为0
		 * 3.lawyerName不为NULL positionNumber不为0
		 */
		
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		//查询结果队列
		List result=new java.util.LinkedList();
		
		//第一种情况
		if(lawyerName.length()==0&&positionNumber!=0){
			result=session.createCriteria(TblLawyer.class)
			.add(Restrictions.eq("positionName", positionNumber))
			.add(Restrictions.eq("deleteLabel", 0))
			.list();
		}
		
		//第二种情况
		if(lawyerName.length()!=0&&positionNumber==0){
			result=session.createCriteria(TblLawyer.class)
			.add(Restrictions.eq("lawyerName", lawyerName))
			.add(Restrictions.eq("deleteLabel", 0))
			.list();
		}
		
		//第三种情况
		if(lawyerName.length()!=0&&positionNumber!=0){
			result=session.createCriteria(TblLawyer.class)
			.add(Restrictions.eq("lawyerName", lawyerName))
			.add(Restrictions.eq("positionName", positionNumber))
			.add(Restrictions.eq("deleteLabel", 0))
			.list();
		}
		
		// 提交事务
		tx.commit();
		session.close();
		return result.size();
	}
	
	
	/**
	 * 修改律师信息
	 * @param tl：修改之后的律师信息对象
	 */
	public void modifyLawyerInfo(TblLawyer tl){
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		
		//获得时间字符串表示
		java.text.DateFormat df=new java.text.SimpleDateFormat("yyyy-MM-dd");
		String dateString=df.format(tl.getBirthTime());
		String hql="update TblLawyer m set m.lawyerName=?,m.lawyerSex=?,m.politicalStatus=?,m.businessNumber=?,m.honestArchives=?,m.lawyerPhone=?,m.educationalLevel=?,m.identityNumber=?,m.annSurRegInfo=?,m.positionName=?,m.graduateSchool=?,m.lawyerEmail=?,m.birthTime=?where m.lawyerId=?"; 
		session.createQuery(hql).setString(0,tl.getLawyerName()) 
		.setString(1,tl.getLawyerSex()) 
		.setString(2,tl.getPoliticalStatus())
		.setString(3, tl.getBusinessNumber())
		.setString(4, tl.getHonestArchives())
		.setString(5, tl.getLawyerPhone())
		.setString(6, tl.getEducationalLevel())
		.setString(7, tl.getIdentityNumber())
		.setString(8, tl.getAnnSurRegInfo())
		.setInteger(9, tl.getPositionName())
		.setString(10, tl.getGraduateSchool())
		.setString(11, tl.getLawyerEmail())
		.setString(12,dateString)
		.setInteger(13, tl.getLawyerId())
		.executeUpdate();
		// 提交事务
		tx.commit();
		session.close();
		
	}
	
	/**
	 * 删除律师信息
	 * @param lawyerId：要被删除的律师信息ID
	 * @return:1：删除成功
	 */
	public int deleteLawyerInfo(int lawyerId){
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		String hql="update TblLawyer m set m.deleteLabel=?where m.lawyerId=?"; 
		session.createQuery(hql) 
		.setInteger(0, 1)
		.setInteger(1, lawyerId)
		.executeUpdate();
		// 提交事务
		tx.commit();
		session.close();
		return 1;
	}
	
	
	/**
	 * 根据实习老师id获取实习老师姓名
	 * @param lawyerId：实习老师ID
	 * @return：实习老师姓名
	 */
	public String getLawyerName(int lawyerId){
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		List result=session.createCriteria(TblLawyer.class)
		.add(Restrictions.eq("lawyerId", lawyerId))
		.list();
		// 提交事务
		tx.commit();
		session.close();
		
		//获得对应的律师对象
		TblLawyer tl=(TblLawyer)result.get(0);
		return tl.getLawyerName();
	}
	
	
	/**
	 * 根据实习老师姓名获取实习老师id
	 * @param lawyerName：实习老师姓名
	 * @return：实习老师ID
	 */
	public int getLawyerId(String lawyerName){
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		List result=session.createCriteria(TblLawyer.class)
		.add(Restrictions.eq("lawyerName", lawyerName))
		.list();
		// 提交事务
		tx.commit();
		session.close();
		
		//获得对应的律师对象
		TblLawyer tl=(TblLawyer)result.get(0);
		return tl.getLawyerId();
	}
	
	

}